package cn.mesmile.flow.modules.processhistory.service.impl;

import cn.mesmile.flow.modules.processhistory.model.dto.HistoryQueryDTO;
import cn.mesmile.flow.modules.processhistory.service.ProcessHistoryService;
import lombok.AllArgsConstructor;
import org.flowable.engine.FormService;
import org.flowable.engine.HistoryService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricDetail;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.history.HistoricVariableUpdate;
import org.flowable.engine.task.Comment;
import org.flowable.identitylink.api.IdentityLinkInfo;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.task.service.impl.persistence.entity.data.impl.cachematcher.TasksByExecutionIdMatcher;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zb
 * @date 2020/12/7 18:12
 * @Description
 */
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS,readOnly = true)
@Service
public class ProcessHistoryServiceImpl implements ProcessHistoryService {

    private final HistoryService historyService;

    private final FormService formService;

    private final TaskService taskService;

    @Override
    public void get(HistoryQueryDTO historyQueryDTO) {
        HistoricProcessInstance historicProcessInstance1 = historyService.createHistoricProcessInstanceQuery().processInstanceId("").singleResult();
        // 5c8eadfb-386c-11eb-8f61-b2359f192e2c
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(historyQueryDTO.getProcessInstanceId())
                .unfinished()
                .orderByHistoricActivityInstanceEndTime().asc()
                .list();

//
//        List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery()
//                .processInstanceId("") // 流程实例id
//                .unfinished() // 未结束的流程节点【当前任务节点】
//                .list();
//        HistoricActivityInstance historicActivityInstance1 = historicActivityInstances.get(0);
        // 活动节点类型
//        String activityType1 = historicActivityInstance1.getActivityType();

        for (HistoricActivityInstance historicActivityInstance : list) {

            String activityId = historicActivityInstance.getActivityId();

            String activityName = historicActivityInstance.getActivityName();

            // 花费时间
            Long durationInMillis = historicActivityInstance.getDurationInMillis();
            // 活动节点类型
            String activityType = historicActivityInstance.getActivityType();
            System.out.println("activityType = " + activityType);

        }

        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(historyQueryDTO.getProcessInstanceId())
                .singleResult();
        String businessKey = historicProcessInstance.getBusinessKey();
        Date endTime = historicProcessInstance.getEndTime();
        Long durationInMillis = historicProcessInstance.getDurationInMillis();
        System.out.println("businessKey = " + businessKey);
        System.out.println("durationInMillis = " + durationInMillis);


        List<HistoricVariableInstance> historicVariableInstances = historyService.createHistoricVariableInstanceQuery()
                .processInstanceId(historyQueryDTO.getProcessInstanceId())
                .list();
        for (HistoricVariableInstance historicVariableInstance : historicVariableInstances) {
            String variableName = historicVariableInstance.getVariableName();
            Object value = historicVariableInstance.getValue();
            String variableTypeName = historicVariableInstance.getVariableTypeName();
            System.out.println(value);
        }

        List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(historyQueryDTO.getProcessInstanceId()).list();


        List<HistoricActivityInstance> list2 = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(historyQueryDTO.getProcessInstanceId())
                .list();

        list2.stream().parallel().forEach(historicActivityInstance -> {

            // ActivityTypes.TASK_USER_TASK  节点类型
            String activityType = historicActivityInstance.getActivityType();

            List<HistoricDetail> historicDetails = historyService.createHistoricDetailQuery()
                    .processInstanceId(historyQueryDTO.getProcessInstanceId())
                    .activityInstanceId(historicActivityInstance.getActivityId()).list();

            for (HistoricDetail historicDetail : historicDetails) {
                if (historicDetail instanceof HistoricVariableUpdate) {
                    HistoricVariableUpdate variableUpdate = (HistoricVariableUpdate) historicDetail;

                    String variableName = variableUpdate.getVariableName();
                    Object value = variableUpdate.getValue();


                }
            }

        });

        /*
        HistoricProcessInstance保存当前及已结束流程实例的信息。
        HistoricVariableInstance保存流程变量或任务变量的最新值。
        HistoricActivityInstance保存活动（流程中的节点）的一次执行的信息。
        HistoricTaskInstance保存当前与历史（完成及删除的）任务实例的信息。
        HistoricIdentityLink保存任务及流程实例、当前及历史的身份关联的信息。
        HistoricDetail保存与历史流程实例、活动实例或任务实例等有关的多种信息
         */

        // 这是所有的流程【实例id】
        List<HistoricVariableInstance> historicVariableInstanceList = historyService
                .createHistoricVariableInstanceQuery()
                .processInstanceId(historyQueryDTO.getProcessInstanceId())
                .taskId("").list();

        for (HistoricVariableInstance historicVariableInstance : historicVariableInstanceList) {
            String variableName = historicVariableInstance.getVariableName();
            Object value = historicVariableInstance.getValue();
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            System.out.println(variableName + " -----------> "+ value);
        }


        /**
         *  HistoricIdentityLink保存任务及流程实例、当前及历史的身份关联的信息。
         */


        List<HistoricTaskInstance> historicTaskInstances = historyService
                .createHistoricTaskInstanceQuery().list();

        for (HistoricTaskInstance historicTaskInstance : historicTaskInstances) {
            Date claimTime = historicTaskInstance.getClaimTime();
            String deleteReason = historicTaskInstance.getDeleteReason();
            Date endTime1 = historicTaskInstance.getEndTime();
            String assignee = historicTaskInstance.getAssignee();
            Date dueDate = historicTaskInstance.getDueDate();
            String category = historicTaskInstance.getCategory();


        }




    }


}
